package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/8 20:48
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    /**
     * 跳转到上传界面
     * @return
     */
    @RequestMapping("/toUpload")
    public  String toUpload(){
        return "fileupload";
    }

    @RequestMapping("/test1")
    @ResponseBody
    public String test1(MultipartFile source, HttpServletRequest request) throws  Exception{
        //1. 获取完整的文件名
        String originalFilename = source.getOriginalFilename();
        //2. 获取文件保存路径
        String realPath = request.getServletContext().getRealPath("/upload");
        System.out.println(realPath);
        source.transferTo(new File(realPath+"\\"+originalFilename));


        //4. 返回文件保存的路径字符串
        return  realPath+"\\"+originalFilename;
    }
}
